package com.css.common.core.utils;

import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class RedisUtils {

    private static StringRedisTemplate stringRedisTemplate;

    private RedisUtils() {

    }

    public static String get(String key) {
        if (GyUtils.isNull(key)) {
            return null;
        }
        return getRedisTemplate().opsForValue().get(key);
    }

    public static List<String> getList(String tableName) {
        if (GyUtils.isNull(tableName)) {
            return null;
        }
        return getRedisTemplate().opsForList().range(tableName, 0, -1);
    }

    private static StringRedisTemplate getRedisTemplate() {
        if (GyUtils.isNull(stringRedisTemplate)) {
            stringRedisTemplate = SpringIocUtils.getBean(StringRedisTemplate.class);
        }
        return stringRedisTemplate;
    }

    public static String getHash(String tableName, String key) {
        if (GyUtils.isNull(tableName) || GyUtils.isNull(key)) {
            return null;
        }
        return (String) getRedisTemplate().opsForHash().get(tableName, key);
    }

    public static void put(String key, String value) {
        if (!GyUtils.isNull(key) && !GyUtils.isNull(value)) {
            getRedisTemplate().opsForValue().set(key, value);
        }
    }

    public static void put(String key, String value, long timeout) {
        TimeUnit unit = TimeUnit.SECONDS;
        if (!GyUtils.isNull(key) && !GyUtils.isNull(value)) {
            getRedisTemplate().opsForValue().set(key, value, timeout, unit);
        }
    }

    public static void put(String key, String value, long timeout, TimeUnit unit) {
        if (!GyUtils.isNull(key) && !GyUtils.isNull(value)) {
            getRedisTemplate().opsForValue().set(key, value, timeout, unit);
        }
    }

    public static void putList(String key, Collection<String> values) {
        if (!GyUtils.isNull(key) && !GyUtils.isNull(values)) {
            RedisUtils.getRedisTemplate().delete(key);
            getRedisTemplate().opsForList().rightPushAll(key, values);
        }
    }


    public static void putHash(String key, Map<String, Object> m) {
        if (!GyUtils.isNull(key) && !GyUtils.isNull(m)) {
            RedisUtils.getRedisTemplate().delete(key);
            getRedisTemplate().opsForHash().putAll(key, m);
        }
    }

    public static void delete(String key) {
        if (!GyUtils.isNull(key)) {
            RedisUtils.getRedisTemplate().delete(key);
        }
    }

    public static boolean lock(String key, String value, long timeout, TimeUnit unit) {
        if (GyUtils.isNull(key)) {
            return false;
        }
        return RedisUtils.getRedisTemplate().opsForValue().setIfAbsent(key, value, timeout, unit);
    }

    public static boolean expire(String key, long timeout, TimeUnit unit) {
        if (GyUtils.isNull(key)) {
            return false;
        }
        return RedisUtils.getRedisTemplate().expire(key, timeout, unit);
    }

    public static Long rpushAll(String key, String... value) {
        return RedisUtils.getRedisTemplate().opsForList().rightPushAll(key, value);
    }

    public static Long llen(String key) {
        return RedisUtils.getRedisTemplate().opsForList().size(key);
    }

    public static String leftPop(String key) {
        return RedisUtils.getRedisTemplate().opsForList().leftPop(key);
    }

    public static boolean exists(String key) {
    	if (GyUtils.isNull(key)) {
            return false;
        }
        Boolean exists = RedisUtils.getRedisTemplate().hasKey(key);
        return exists != null ? exists : false;
    }



}
